package com.lxj.student_security.controller;

import com.lxj.student_security.common.CommonResult;
import com.lxj.student_security.entity.LoginParam;
import com.lxj.student_security.service.UserService;
import com.lxj.student_security.utils.JwtTokenUtil;
import io.swagger.models.auth.In;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.HashSet;

@RestController
@RequestMapping("/login")
public class LoginController {
    @Value("${jwt.tokenHead}")
    private String tokenHead;
    @Autowired
    private UserService userService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @PostMapping("/verify")
    public CommonResult verify(LoginParam loginParam) {
        HashMap<String, String> map = new HashMap<>();
        String token = null;
        Jedis jedis = new Jedis();
        String code = jedis.get("code");
        String count = null;
        boolean exists = jedis.exists(loginParam.getUsername());
        if (exists){
            count = jedis.get(loginParam.getUsername());
        }else {
            jedis.set(loginParam.getUsername(), "0");
        }
        if (Integer.parseInt(count)>=3){
            jedis.expire("loginStatus",86400);
            return CommonResult.failed("该账户已被冻结，请联系管理员进行解冻");
        }else {
            if (loginParam.getCode().equals(code)){
                try {
                    token = userService.login(loginParam);
                } catch (Exception e) {
                    e.printStackTrace();
                    return CommonResult.validateFailed("用户名或密码错误");
                }
                if (StringUtils.isEmpty(token)){
                   jedis.set(loginParam.getUsername(), String.valueOf(Integer.parseInt(count)+1));
                    return CommonResult.validateFailed("用户名或密码错误");
                }
                map.put("tokenHead", tokenHead);
                map.put("access_token", token);
                jedis.set("username",loginParam.getUsername());
                return CommonResult.success(map);
            }
            return CommonResult.failed("验证码错误");
        }
    }
    @PostMapping("/freeze")
    public CommonResult freeze(String username){
        Jedis jedis = new Jedis();
        if (jedis.exists(username)){
            if (Integer.parseInt(jedis.get(username))>=3){
                jedis.set(username,"0");
                return CommonResult.success(200);
            }
            jedis.set(username,"4");
            return CommonResult.success(100);
        }
        jedis.set(username,"4");
            return CommonResult.success(100);
    }
    @PostMapping("/getName")
    public CommonResult getName(){
        Jedis jedis = new Jedis();
        String username = jedis.get("username");
        return CommonResult.success(username);
    }
}
